<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">
      <el-form-item style="width:100%" class="oneCls" label="接口名称" prop="interfaceName">
        <el-input @input="changeUserInput(dataForm.interfaceName)" style="width:85%" v-model="dataForm.interfaceName" placeholder=""></el-input>
        <p class="info">
          <!-- <span>说明：输入的内容是纯英文，包含大小写</span> -->
          <span style="color:red;">{{ textInfo }}</span>
        </p>
        <el-button :disabled="enFlag" :type="enFlag?'success':'primary'" @click="yzParmData(dataForm.interfaceName)">
          <span v-if="enFlag"><i class="el-icon-success"></i> 通过</span>
          <span v-else><i class="el-icon-menu"></i>验证</span>
        </el-button>
      </el-form-item>
      <el-form-item v-if="enFlag" label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder=""></el-input>
      </el-form-item>
      <el-form-item v-if="enFlag" label="表名称" prop="url">
        <el-select @change="changeData" v-model="dataForm.tableId" placeholder="请选择">
          <el-option
            v-for="(item,index) in options"
            :key="index"
            :label="item.name"
            :value="item.tableName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width:100%" v-if="enFlag" label="sql语句" prop="sqlStr">
        <el-input v-model="dataForm.sqlStr" type="textarea" placeholder=""></el-input>
      </el-form-item>
      <el-form-item style="width:100%" v-if="enFlag" label="列值" prop="field">
        <el-input v-model="dataForm.field"  type="textarea" placeholder=""></el-input>
      </el-form-item>
      <el-form-item v-if="enFlag" label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>


    <el-dialog
      width="30%"
      title="JSON参数编辑"
      :visible.sync="editPostJsonFlag"
      append-to-body>
      <div v-if="editPostJsonFlag">
        <vue-json-editor
          v-model="resultInfo"
          :showBtns="false"
          :mode="'code'"
          @json-change="onJsonChange"
          @json-save="onJsonSave"
          @has-error="onError"
        />
        <br>
      <el-button type="primary" @click="checkJson">确定</el-button>
    </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
    import request from '@/utils/request'
 import vueJsonEditor from 'vue-json-editor'
  export default {
    data () {
      return {
        options:[],
        hasJsonFlag:false,
        // json数据
        resultInfo: {},
        textInfo:"",
        enFlag:false,
        visible: false,
        jsonType:'',
        editPostJsonFlag: false,
        dataForm: {
          name:null,
          tableId:"",
          interfaceName:null,
          sqlStr:"",
          note:"",
          field:""
        },
        dataRule: {
          name: [
            { required: false, message: '请输入名称', trigger: 'change'}
          ],
          interfaceName: [
            { required: true, message: '请输入接口名称', trigger: 'change'}
          ],
        }
      }
    },
    components:{
      vueJsonEditor
    },
    mounted(){
      this.$nextTick(()=>{
        this.getSelectDataList()
      })
    },
    methods: {
      changeData(v){
        request({
          url: '/ht-datadevelop/table/config/getTableNameToSQL',
          method: 'post',
          data:{tableName:this.dataForm.tableId}
        }).then(({data}) => {
          if (data && data.code === 0) { 
            this.dataForm.sqlStr = data.data.sqlStr
            this.dataForm.field = data.data.field
          }
        })
      },
      getSelectDataList(){
        request({
          url: '/ht-datadevelop/table/config/getListToConfig',
          method: 'get',
          params: {}
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.data
          }
        })
      },
      onJsonChange (value) {
        // 实时保存
        this.onJsonSave(value)
      },
      onError(value) {
        this.hasJsonFlag = false
      },
      //确定按钮 检查json
      checkJson(){
        this.dataForm[this.jsonType] = JSON.stringify(this.resultInfo)
        this.editPostJsonFlag = false
        return true
      },
      // 输入值的时候调用
      onJsonSave (value) {
        this.resultInfo = value
        this.hasJsonFlag = true
      },
      // input框获得焦点
      editJson (v,jsonObj) {
        if(jsonObj){  
          this.resultInfo = JSON.parse(jsonObj) //json编辑器数据
        }else{
          this.resultInfo = {}
        }
        this.jsonType = v
        this.editPostJsonFlag = true
      },
      reset(v){
        this.dataForm = {
          tableId:"",
          name:null,
          interfaceName:null,
          sqlStr:"",
          note:"",
          field:""
        }
        this.visible = false
      },
      changeUserInput(v){
        this.enFlag = false
        if(v.length==0){
          this.textInfo = '请先输入英文名称'
          return 
        }else{
          this.textInfo = ''
        }
      },
      yzParmData(v){
        if(this.enFlag){
          return 
        }
        this.dataForm['tableId'] = ""
        this.dataForm['name'] = ""
        this.dataForm['sqlStr'] = ""
        this.dataForm['note'] = ""
        this.dataForm['field'] = ""
        if(!v||v.length==0){
          this.textInfo = '请先输入英文名称'
          return 
        }
        request({
          url: '/ht-datadevelop/table/config/getInterfaceNameCompliance',
          method: 'post',
          data:{interfaceName:v}
        }).then((res) => {
          if(res.data.success){
            this.enFlag = true
            this.textInfo = ""
          }else{
            this.textInfo = res.data.message
          }
        })
      },
     
      init (row) {
        if(row){
          this.textInfo = ""
          let obj = {...row}
          obj.jsonHeard = obj.jsonHeard==null?null:JSON.stringify(obj.jsonHeard)
          obj.jsonBody = obj.jsonBody==null?null:JSON.stringify(obj.jsonBody)
          this.dataForm = JSON.parse(JSON.stringify(obj)) 
          this.enFlag = true
        }else{
          this.enFlag = false
        }
        this.visible = true
      },
      // 表单提交
      dataFormSubmit () {
        if(!this.enFlag){
          return 
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let a= this.dataForm.tableId
            let strId = ""
            this.options.forEach((item,index)=>{
              if(item.tableName==a){
                strId = item.table_id
              }
            })
            this.dataForm.tableId = strId
            request({
              url: `/ht-datadevelop/table/config/interfaceConfigSaveOrUpdate`,
              method: 'post',
              data:this.dataForm
            }).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.reset()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>


<style lang="scss" scoped>
>>>.el-select{
  width:100% !important;
}
>>>.el-textarea>textarea{
  min-height:100px !important;
}
>>>.info{
    position: absolute;
    left: 0;
    top: 18px;
    font-size: 10px;
    width:100%;
    color: rgb(75, 103, 240);
  }
  .oneCls{
    >div{
      position: relative;
      >button{
        position: absolute;
        right: 0;
      }
    }
  }
.editContent{
  border-radius: 10px;
  padding:10px;
  border:1px solid #eee;

  >div{
    max-height: 300px;
    overflow-y: scroll;
    .p2{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    margin: 5px 0 3px 0;
    .el-input{
      width: 22%;
      margin:2px;
      box-sizing: border-box;
    }
    span{
      position: relative;
      top:5px;
      margin-left:16px;
      i{
        cursor: pointer;
      }
    }
  }
  }
  .p1{
    margin: 0;
    label{
      display: inline-block;
      width: 22%;
      text-align: center;
      background: #eee;
      font-weight: bold;
    }
  }
}
  .el-form{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .el-form-item{
      width:50%;
    }
  }
</style>